/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cq.base.domain.KeyLabel;
import com.cq.base.domain.Page;
import com.cq.bd.dao.BInventoryDao;
import com.cq.bd.dao.BTakeDao;
import com.cq.bd.dao.jdbc.BTakeJdbcDao;
import com.cq.bd.domain.BInventory;
import com.cq.bd.domain.BTake;
import com.cq.bd.service.TakeSerive;

/**
 * 采血服务
 * 
 * @author huangmy
 * @date   2009-10-5
 */
public class TakeSeriveImpl implements TakeSerive {
    private BTakeDao bTakeDao;
    private BTakeJdbcDao bTakeJdbcDao;
    private BInventoryDao bInventoryDao;
    
    public List<BInventory> queryBloodResult() {        
        return this.bTakeJdbcDao.queryBloodResult();
    }
    
    public Page pageCondTakes(Map<String, String> filter, Map<String, String> like, Map<String, String> sort,
            int pageNo, int pageSize) {        
        return this.bTakeJdbcDao.pageCondTakes(filter, like, sort, pageNo, pageSize);
    }
    
    public List<KeyLabel> queryTakeTypeCount() {
        return this.bTakeJdbcDao.queryBloodTypeCounts();
    }
    
    /**
     * 采血登记
     * 
     * @param take
     */
    public void doTake(BTake take) {
        this.bTakeDao.create(take);
        List<BInventory> bis = this.bInventoryDao.findBy("bloodType", take.getBloodType());
        if (bis == null || bis.size() <= 0) {
            BInventory bi = new BInventory();
            bi.setBloodType(take.getBloodType());
            bi.setBloodVolume(take.getBloodVolume());
            
            this.bInventoryDao.create(bi);
        } else {
            BInventory bi = bis.get(0);
            bi.setBloodVolume(bi.getBloodVolume() + take.getBloodVolume());
            
            this.bInventoryDao.create(bi);
        }
    }
    
    public BTake doGetTake(Long bloodId) {
        return this.bTakeDao.get(bloodId);
    }
    
    public Page pageTakes(Map<String, String> filter, Map<String, String> like, Map<String, String> sort, int pageNo,
            int pageSize) {
        filter.remove("beginTime");
        filter.remove("endTime");
        return this.bTakeDao.pagedQuery(filter, like, sort, pageNo, pageSize);
    }
    
    /**
     * 修改采血记录
     * 
     * @param take
     */
    public void doUpdateTake(BTake take) {
        this.bTakeDao.update(take);
    }
    
    /**
     *  获取所有的采血记录
     * 
     * @return
     */
    public List<BTake> doGetTakes() {
        return this.bTakeDao.list();
    }
    
    /**
     * 获取采血记录  姓名
     * 
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByName(String name) {
        return this.bTakeDao.find("from BTake from bloodDonor like '%" + name + "%'");
    }
    
    /**
     * 获取采血记录  身份证号码
     * 
     * @param num
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByNum(String num) {
        return this.bTakeDao.find("from BTake from bloodNum like '%" + num + "%'");
    }
    
    /**
     * 获取采血记录  血型
     * 
     * @param num
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByType(String type) {
        return this.bTakeDao.find("from BTake from bloodType=?", type);
    }
    
    /**
     * 获取采血记录  采血者
     * 
     * @param num
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByWorkerId(Long workerId) {
        return this.bTakeDao.find("from BTake from workerId=?", workerId);
    }
    
    /**
     * 采血记录数 采血者
     * 
     * @param workerId
     * @return
     */
    public int doQueryTakesNumByWorkerId(Long workerId) {
        return this.bTakeDao.getCount("from BTake from workerId=?", workerId);
    }
    
    /**
     * 采血记录数 采血时间
     * 
     * @param beginTime
     * @param endTime
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByTakeTime(Date beginTime, Date endTime) {
        return this.bTakeDao.find("from BTake from takeTime>=? and takeTime<=?", beginTime, endTime);
    }
    
    /**
     * 采血记录数 采血地点
     * 
     * @param locationId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<BTake> doGetTakesByTakeLocation(Long locationId) {
        return this.bTakeDao.find("from BTake from takeLocation=?", locationId);
    }

    public void setBTakeDao(BTakeDao takeDao) {
        bTakeDao = takeDao;
    }

    public void setBTakeJdbcDao(BTakeJdbcDao takeJdbcDao) {
        bTakeJdbcDao = takeJdbcDao;
    }

    public void setBInventoryDao(BInventoryDao inventoryDao) {
        bInventoryDao = inventoryDao;
    }
}
